package com.cjw.service.impl.operationAnalysis;

import com.cjw.mapper.livesquare.*;
import com.cjw.mapper.soulread.ReadMapper;
import com.cjw.pojo.dto.operationAnalysis.BarDTO;
import com.cjw.pojo.entity.livesquare.CommentParent;
import com.cjw.pojo.entity.livesquare.Plan;
import com.cjw.service.operationAnalysis.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author : cjw
 * @date: : 2022/3/11 20:49
 * @description : CommunityServiceImpl
 */
@Service
public class CommunityServiceImpl implements CommunityService {
    @Autowired
    private PlanMapper planMapper;

    @Autowired
    private UserPlanMapper userPlanMapper;

    @Autowired
    private ReadMapper readMapper;

    @Autowired
    private LiveUserMapper liveUserMapper;

    @Autowired
    private LiveSharingMapper liveSharingMapper;

    @Autowired
    private CommentLiveParentMapper parentMapper;

    @Autowired
    private CommentLiveSonMapper sonMapper;

    @Override
    public List<BarDTO> plan(Date date) {
        ArrayList<BarDTO> barDTOArrayList = new ArrayList<>();
        BarDTO barDTO = new BarDTO();
        barDTO.setName("参与");
        barDTO.setValue(userPlanMapper.getAllCountByTime(date));
        barDTOArrayList.add(barDTO);

        Plan plan = new Plan();
        BarDTO barDTO1 = new BarDTO();
        barDTO1.setName("进行中");
        plan.setState(0);
        barDTO1.setValue(planMapper.selectCount(plan));
        barDTOArrayList.add(barDTO1);

        BarDTO barDTO2 = new BarDTO();
        plan.setState(1);
        barDTO2.setName("已结束");
        barDTO2.setValue(planMapper.selectCount(plan));
        barDTOArrayList.add(barDTO2);

        BarDTO barDTO3 = new BarDTO();
        plan.setState(2);
        barDTO3.setName("已取消");
        barDTO3.setValue(planMapper.selectCount(plan));
        barDTOArrayList.add(barDTO3);
        return barDTOArrayList;
    }

    @Override
    public List<BarDTO> read(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        ArrayList<BarDTO> barDTOS = new ArrayList<>();
        SimpleDateFormat format=new SimpleDateFormat("dd日");
        for (int i=0;i>-5;i--) {
            BarDTO barDTO = new BarDTO();
            barDTO.setValue(readMapper.findCountByDate(calendar.getTime()));
            barDTO.setName(format.format(calendar.getTime()));
            barDTOS.add(barDTO);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }
        return barDTOS;
    }

    @Override
    public List<List> share(Date date) {
        List<List> lists = new ArrayList<>();
        ArrayList<String> days = new ArrayList<>();
        ArrayList<Integer> live = new ArrayList<>();
        ArrayList<Integer> collection = new ArrayList<>();
        ArrayList<Integer> love = new ArrayList<>();
        ArrayList<Integer> comment = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format=new SimpleDateFormat("dd日");
        calendar.setTime(date);

        for (int i=0;i<7;i++) {
            int comments = 0;
            days.add(format.format(calendar.getTime()));
            live.add(liveSharingMapper.getCountByDate(calendar.getTime()));
            collection.add(liveUserMapper.getCountByDate(calendar.getTime(), 2));
            love.add(liveUserMapper.getCountByDate(calendar.getTime(), 1));
            List<Long> countByDate = parentMapper.getCountByDate(calendar.getTime());
            for (Long id : countByDate) {
                comments += sonMapper.getCountByDate(calendar.getTime(), id);
            }
            comments += countByDate.size();
            comment.add(comments);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }

        lists.add(days);
        lists.add(live);
        lists.add(comment);
        lists.add(love);
        lists.add(collection);
        return lists;
    }


}
